package com.qf.iot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.iot.Mapper.QfDeviceMapper;
import com.qf.iot.dto.PageDto;
import com.qf.iot.entity.QfDevice;
import com.qf.iot.entity.QfDevice;
import com.qf.iot.service.QfDeviceService;
import com.qf.iot.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 作者：肖骏麟
 * 时间:2023/10/29 23:09
 * 描述：优化小子
 */
@Service
public class QfDeviceServiceImpl extends ServiceImpl<QfDeviceMapper, QfDevice> implements QfDeviceService {

    @Autowired
    private QfDeviceMapper qfDeviceMapper;

    public PageResult<QfDevice> page(PageDto<QfDevice> pageDto) {
        //分页信息
        IPage<QfDevice> page = new Page<>(pageDto.getNow(),pageDto.getSize());

        //条件
        QueryWrapper<QfDevice> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(pageDto.getWhere().getName())){
            queryWrapper.lambda().like(QfDevice::getName,pageDto.getWhere().getName());
        }
        if(!StringUtils.isEmpty(pageDto.getWhere().getDeviceKey())){
            queryWrapper.lambda().eq(QfDevice::getDeviceKey,pageDto.getWhere().getDeviceKey());
        }

        IPage<QfDevice> pageInfo = this.baseMapper.selectPage(page, queryWrapper);
        //封装分页结果
        PageResult<QfDevice> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setList(pageInfo.getRecords());
        return pageResult;
    }

    @Override
    public List<QfDevice> findDeviceList(String name) {
        List<QfDevice> deviceList = qfDeviceMapper.findDeviceList(name);
        return deviceList;
    }

    @Override
    public List<QfDevice> findListByDeviceName(String name) {
        List<QfDevice> list = qfDeviceMapper.findAllDeviceTypeProduct(name);
        return list;
    }
}
